import request from '@common/utils/request';
import constants from "./constants";
import utils from '@common/utils/utils';
import { message } from '@common/assets/js/resetMessage';
import store from '@/store';

const common = {
    requestData: (url, data = {}, success, error, fail, addLog = true, headers = {}) => {
        return request({
            url: url,
            method: 'post',
            data,
            withCredentials: true,
            timeout: data && data.timeout ? data.timeout : window.configs.REQUEST_TIMEOUT,
            headers,
        }).then(res => {
            if (utils.isNotEmpty(res.errno)) {
                if (res.errno === 0) {
                    if (success && typeof success === 'function') {
                        let retVal = success(res);
                        if (retVal !== undefined) {
                            return retVal;
                        }
                    }
                    return res;
                } else if (res.errno === 501) {
                    if (store.state.appLedger) {
                        window.parent.postMessage({
                            cmd: 'appLogout',
                            message: res.message,
                        }, '*');
                    } else {
                        utils.logout();
                        store.dispatch('errorLog/clearErrorLog');
                        location.hash = '/login';
                    }
                } else {
                    store.dispatch('autoAddErrorLog', res);
                    if (error) {
                        typeof error === 'function' && error(res);
                    } else {
                        message.error(res.message);
                    }
                }
                return res;
            }
            if (res.code === 0) {
                if (success && typeof success === 'function') {
                    let retVal = success(res);
                    if (retVal !== undefined) {
                        return retVal;
                    }
                }
                return res;
            } else if (res.code === 1000) {
                if (store.state.appLedger) {
                    window.parent.postMessage({
                        cmd: 'appLogout',
                        message: res.message,
                    }, '*');
                } else {
                    utils.logout();
                    store.dispatch('errorLog/clearErrorLog');
                    location.hash = '/login';
                }
            } else {
                if (addLog) {
                    store.dispatch('autoAddErrorLog', res);
                }
                if (error) {
                    typeof error === 'function' && error(res);
                } else {
                    message.error(res.message);
                }
            }
            return res;
        }).catch(reason => {
            if (fail) {
                typeof fail === 'function' && fail(reason);
            } else {
                message.error(reason.message);
            }
            return Promise.reject(reason);
        });
    },
    uploadFormData: (url, formData, success, error, fail) => {
        return request.post(url, formData, {
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        }).then(res => {
            if (res.code === 0) {
                if (success && typeof success === 'function') {
                    let retVal = success(res);
                    if (retVal !== undefined) {
                        return retVal;
                    }
                }
                return res;
            } else if (res.code === 1000) {
                store.dispatch('autoAddErrorLog', res);
                message.error(res.message);
                // utils.logout();
                // location.hash = '/login';
            } else {
                store.dispatch('autoAddErrorLog', res);
                if (error) {
                    typeof error === 'function' && error(res);
                } else {
                    message.error(res.message);
                }
            }
            return Promise.reject(res);
        }).catch(reason => {
            if (fail) {
                typeof fail === 'function' && fail(reason);
            } else {
                message.error(reason.message);
            }
            return Promise.reject(reason);
        });
    },
    exportData(url, data, config, success, error) {
        return request.post(url, data, config).then(res => {
            if (success && typeof success === 'function') {
                let retVal = success(res);
                if (retVal !== undefined) {
                    return retVal;
                }
            }
            return res;
        }).catch(res => {
            store.dispatch('autoAddErrorLog', res);
            if (error) {
                typeof error === 'function' && error(res);
            } else {
                message.error(res.message);
            }
            return Promise.reject(res);
        });
    },
    getData: (url, success, error) => {
        return request({
            url: url,
            method: 'get',
            data: {},
            responseType: 'arraybuffer'
        }).then(res => {
            if (success && typeof success === 'function') {
                let retVal = success(res);
                if (retVal !== undefined) {
                    return retVal;
                }
            }
            return res;
        }).catch(reason => {
            if (error) {
                typeof error === 'function' && error(reason);
            } else {
                message.error(reason.message);
            }
            return Promise.reject(reason);
        });
    },
    getDataText: (url, success, error) => {
        return request({
            url: url,
            method: 'get',
            data: {},
            responseType: 'text/html'
        }).then(res => {
            if (success && typeof success === 'function') {
                let retVal = success(res);
                if (retVal !== undefined) {
                    return retVal;
                }
            }
            return res;
        }).catch(reason => {
            if (error) {
                typeof error === 'function' && error(reason);
            } else {
                message.error(reason.message);
            }
            return Promise.reject(reason);
        });
    },
    login: (data, success, error, fail) => {
        return common.requestData(constants.API_Login, data, success, error, fail);
    },
    refreshToken: (data, success, error, fail) => {
        return common.requestData(constants.API_RefreshToken, data, success, error, fail);
    },
    forward: (data, success, error, fail) => {
        return common.requestData(constants.API_Forward, data, success, error, fail);
    },
    getNowStrWithFormat: (data, success, error, fail) => {
        return common.requestData(constants.API_GetNowStrWithFormat, data, success, error, fail);
    },
    getMillisFromBaseTime: (data, success, error, fail) => {
        return common.requestData(constants.API_GetMillisFromBaseTime, data, success, error, fail);
    },

};
export default common
